package work.linruchang.mybatisplussamplestest.config.mybatisplus;

import cn.hutool.db.sql.SqlUtil;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.p6spy.P6SpyLogger;

/**
 * 作用：
 *
 * @author LinRuChang
 * @version 1.0
 * @date 2022/06/16
 * @since 1.8
 **/
public class EnhaceP6SpyLogger extends P6SpyLogger {

    /**
     * 参考com.baomidou.mybatisplus.extension.p6spy.P6SpyLogger#formatMessage(int, java.lang.String, long, java.lang.String, java.lang.String, java.lang.String, java.lang.String)
     * @param connectionId the id of the connection
     * @param now the current ime expressing in milliseconds
     * @param elapsed the time in milliseconds that the operation took to complete
     * @param category the category of the operation
     * @param prepared the SQL statement with all bind variables replaced with actual values
     * @param sql the sql statement executed
     * @param url the database url where the sql statement executed
     * @return
     */
    @Override
    public String formatMessage(int connectionId, String now, long elapsed, String category, String prepared, String sql, String url) {
        //格式化SQL
        String sqlContent = sql.replaceAll("[\\s]+", " ");
        sqlContent = SqlUtil.formatSql(sqlContent);

        return StringUtils.isNotBlank(sql) ? " Consume Time：" + elapsed + " ms " + now +
                "\n Execute SQL：\n" + sqlContent + "\n" : "";

    }
}
